﻿using Error_Handler_Control;
using PollSystem.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PollSystem.Registered
{
    public partial class EditQuestion : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                int questionId = Convert.ToInt32(Request.Params["questionId"]);
                PollSystemEntities context = new PollSystemEntities();
                if (questionId > 0)
                {
                    using (context)
                    {
                        Question question = context.Questions.Find(questionId);
                        this.TextBoxEditQuestion.Text = question.QuestionText;
                        this.RepeaterAnswers.DataSource = question.Answers.ToList();
                        this.DataBind();
                    }
                }
                else
                {
                    Response.Redirect("ViewAllResults.aspx");
                }
            }
        }

        protected void LinkButtonSaveQuestion_Click(object sender, EventArgs e)
        {
            int questionId = Convert.ToInt32(Request.Params["questionId"]);

            using (PollSystemEntities context = new PollSystemEntities())
            {
                try
                {
                    Question question = context.Questions.Find(questionId);
                    question.QuestionText = this.TextBoxEditQuestion.Text;
                    context.SaveChanges();
                    ErrorSuccessNotifier.ShowAfterRedirect = true;
                    ErrorSuccessNotifier.AddSuccessMessage("Question edited.");
                    Response.Redirect("ViewAllResults.aspx", false);
                }
                catch (Exception ex)
                {
                    ErrorSuccessNotifier.AddErrorMessage(ex);
                    return;
                }
            }
        }

        protected void LinkButtonCancelEdit_Click(object sender, EventArgs e)
        {
            ErrorSuccessNotifier.ShowAfterRedirect = true;
            ErrorSuccessNotifier.AddSuccessMessage("You canceled editing.");
            Response.Redirect("ViewAllResults.aspx", false);
        }

        protected void LinkButtonDelete_Click(object sender, EventArgs e)
        {
            int questionId = Convert.ToInt32(Request.Params["questionId"]);

            using (PollSystemEntities context = new PollSystemEntities())
            {
                try
                {
                    Question question = context.Questions.Find(questionId);
                    context.Answers.RemoveRange(question.Answers);
                    context.Questions.Remove(question);
                    context.SaveChanges();
                    ErrorSuccessNotifier.ShowAfterRedirect = true;
                    ErrorSuccessNotifier.AddSuccessMessage("Question deleted.");
                    Response.Redirect("ViewAllResults.aspx", false);
                }
                catch (Exception ex)
                {
                    ErrorSuccessNotifier.AddErrorMessage(ex);
                }
            }
        }

        protected void ShowCreateNewAnswerForm_Click(object sender, EventArgs e)
        {
            this.FormViewCreateAnswer.Visible = true;
            this.LinkButtonShowCreateAnswerForm.Visible = false;
        }

        protected void LinkButtonSave_Click(object sender, EventArgs e)
        {
            TextBox textBoxAnswer = (TextBox)this.FormViewCreateAnswer.FindControl("TextBoxAnswer");
            TextBox textBoxVotes = (TextBox)this.FormViewCreateAnswer.FindControl("TextBoxVotes");

            using (PollSystemEntities context = new PollSystemEntities())
            {
                int questionId = Convert.ToInt32(Request.Params["questionId"]);
                Answer answer = new Answer();
                answer.QuestionId = questionId;

                if (!string.IsNullOrEmpty(textBoxAnswer.Text))
                {
                    answer.AnswerText = textBoxAnswer.Text;
                }
                else
                {
                    ErrorSuccessNotifier.AddErrorMessage("Please enter answer.");
                }
                if (!string.IsNullOrEmpty(textBoxVotes.Text))
                {
                    try
                    {
                        answer.Votes = int.Parse(textBoxVotes.Text);
                    }
                    catch (Exception ex)
                    {
                        ErrorSuccessNotifier.AddErrorMessage("Please enter number >= 0");
                    }
                }
                else
                {
                    ErrorSuccessNotifier.AddErrorMessage("Please enter number >= 0");
                }
                try
                {
                    context.Answers.Add(answer);
                    context.SaveChanges();
                    ErrorSuccessNotifier.ShowAfterRedirect = true;
                    ErrorSuccessNotifier.AddSuccessMessage("Answer created.");
                    Response.Redirect("EditQuestion?questionId=" + questionId, false);
                }
                catch (Exception ex)
                {
                    ErrorSuccessNotifier.AddErrorMessage(ex);
                }
            }
        }

        protected void LinkButtonCancel_Click(object sender, EventArgs e)
        {
            this.FormViewCreateAnswer.Visible = false;
            this.LinkButtonShowCreateAnswerForm.Visible = true;
            ErrorSuccessNotifier.AddInfoMessage("Creating answer canceled.");
        }

        protected void DeleteAnswer_Command(object sender, CommandEventArgs e)
        {
            int questionId = Convert.ToInt32(Request.Params["questionId"]);
            int answerId = Convert.ToInt32(e.CommandArgument);

            using (PollSystemEntities context = new PollSystemEntities())
            {
                Answer answer = context.Answers.Find(answerId);
                if (answer != null)
                {
                    try
                    {
                        context.Answers.Remove(answer);
                        context.SaveChanges();
                        ErrorSuccessNotifier.ShowAfterRedirect = true;
                        ErrorSuccessNotifier.AddSuccessMessage("Answer deleted.");
                        Response.Redirect("EditQuestion?questionId=" + questionId, false);
                    }
                    catch (Exception ex)
                    {
                        ErrorSuccessNotifier.AddErrorMessage(ex);
                    }
                }
                else
                {
                    ErrorSuccessNotifier.AddErrorMessage("Neshto se oburka pi4");
                }

            }
        }
    }
}